package com.polaris.live.ui.me.beans

import android.os.Bundle
import android.view.View
import androidx.lifecycle.viewModelScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.polaris.live.R
import com.polaris.live.adapter.message.WithdrawDetailAdapter
import com.polaris.live.common.constant.BizEventConst
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.event.EventMessage
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.manager.CoroutineScopeManager
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.common.network.parseState
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.ActivityMyWithdrawDetailBinding
import com.polaris.live.manager.AppConfigManager
import com.polaris.live.resp.back_resp.WithdrawAccountInfoVo
import com.polaris.live.resp.go_resp.SaveWithdrawAccountBean
import com.polaris.live.utils.CommonUtils.toBoolean
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.utils.route.RouterUtils
import com.polaris.live.viewmodel.GlobalViewModelProxy
import com.polaris.live.viewmodel.MyBeansViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.greenrobot.eventbus.EventBus

/**
 * 提现详情页面
 *
 * @author Created by 长空 on 2023/10/26/024 09:42
 */
class MyWithdrawDetailActivity :
    BaseVbActivity<MyBeansViewModel, ActivityMyWithdrawDetailBinding>() {

    private var deleteIndex = -1

    private val mAdapter by lazy { WithdrawDetailAdapter() }

    private var withdrawAccountInfoVo: WithdrawAccountInfoVo? = null

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.recyclerView.apply {
            layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
            adapter = mAdapter
        }

        withdrawAccountInfoVo = intent?.getParcelableExtra(CommonConst.Withdraw.Withdraw_DATA)

        initListener()
        initObserver()
    }

    private fun initListener() {
        mBinding.viewAddWithdraw.setOnSingleClickListener {
            AppConfigManager.getConfig(CommonConst.Configs.ossUrl)?.let {
                RouterUtils.routerGoto(
                    "${it}/static/app/index.html#/withdrawal"
                )
            }
        }

        mAdapter.setOnItemClickListener { _, _, position ->
            if (mAdapter.data[position].status != 0) {
                mAdapter.clearSelected()
                mAdapter.setSelect(position)
                EventBus.getDefault()
                    .post(EventMessage(BizEventConst.WITHDRAW_DETAIL, mAdapter.data[position]))
                mViewModel.saveWithdraw(SaveWithdrawAccountBean(mAdapter.data[position].accountInfoId))
                mViewModel.viewModelScope.launch {
                    delay(200)
                    withContext(CoroutineScopeManager.mainDispatcher) {
                        finish()
                    }
                }
            }
        }

        mAdapter.setOnItemChildClickListener { _, view, position ->
            when (view.id) {
                R.id.iv_delete -> {
                    deleteIndex = position
                    mViewModel.deleteWithdraw(mAdapter.data[position].accountInfoId)
                }

                R.id.iv_edit_user -> {
                    val item = mAdapter.getItem(position)
                    AppConfigManager.getConfig(CommonConst.Configs.ossUrl)?.let {
                        RouterUtils.routerWeb(
                            "${it}/static/app/index.html#/withdrawal",
                            mapOf("accountInfoId" to item.accountInfoId.toString())
                        )
                    }
                }
            }
        }
    }

    private fun initObserver() {
        mViewModel.mWithdrawAccountLiveData.observe(this) { resultState ->
            parseState(resultState, {
                var isNeedInitSelect = mAdapter.data.isEmpty()
                mAdapter.setList(it.withdrawAccountInfoVo)
                if (mAdapter.data.size == 4) {
                    mBinding.groupAdd.visibility = View.GONE
                }
                val index = mAdapter.data.indexOf(withdrawAccountInfoVo)
                if (isNeedInitSelect) {
                    mAdapter.clearSelected()
                    mAdapter.setSelect(index)
                }
            }, {
                ToastGravity.showGravity(it.errorMsg)
            })
        }
        mViewModel.mDeleteWithdrawLiveData.observe(this) { resultState ->
            parseState(resultState, {
                mBinding.groupAdd.visibility = View.VISIBLE
                mAdapter.removeAt(deleteIndex)
                val index = mAdapter.data.indexOf(withdrawAccountInfoVo)
                if (index >= 0) {
                    mAdapter.clearSelected()
                    mAdapter.setSelect(index)
                } else {
                    mAdapter.clearSelected()
                    EventBus.getDefault().post(EventMessage(BizEventConst.WITHDRAW_DETAIL_DELETE))
                }
            }, {
                ToastGravity.showGravity(it.errorMsg)
            })
        }
        GlobalViewModelProxy.getCustomerService(mViewModel) { resultState ->
            parseState(resultState, { resp ->
                if (resp.display.toBoolean()) {
                    mBinding.bvTitle.setRightResource(R.drawable.ic_kefu_black)
                    mBinding.bvTitle.setOnRightClientListener {
                        resp.url?.let { RouterUtils.routerCustomerService(ImageUtils.imgPath(it)) }
                    }
                }
            })
        }
    }

    override fun onResume() {
        super.onResume()
        mViewModel.getWithdraw()
    }

}